import { bg_image } from "./indoorConfig.js";
import {userCarList,getUserInfo} from "../api/home/index.js"
import {parkInsideList} from "../api/scheme/index.js"
export default {
	data() {
		return {
			bg_image,
			list: [],
			flagExist: false //是否存在已绑定车牌
		}
	},
	onLoad() {
		this.getParkInsideList()
		
	},
	onShow(){
		this.fetchSomething()
	},
	onPullDownRefresh(){//页面下拉刷新
		this.fetchSomething()
		uni.stopPullDownRefresh();
	},
	methods: {
		async fetchSomething(query){
			this.getParkInsideList()
			this.getCarList()
		},
		async getCarList() {
			let res = await userCarList()
			let data = res.data || []
			if (data.length) {
				this.flagExist = true
			} 
		},
		beforeBuyMonthCard(){
			if(!this.flagExist){
				uni.showModal({
					title: '友情提示',
					content: `暂未绑定车牌，点击下方《去绑定》进行车牌绑定`,
					confirmText: '去绑定',
					confirmColor: '#2770FE',
					showCancel: true,
					success: (res) => {
						if (res.confirm) {
							uni.navigateTo({
								url: `/pages/indoor/myCar?redirect=1`
							})
						}
					}
				})
				return false
			}
			//判断用户是否绑定手机号
			this.getUserinfo()
		},
		async getUserinfo(){
			let res = await getUserInfo()
			let data = res.data || {}
			uni.setStorageSync('userInfo',data)
			if(!data.phoneNumber){
				uni.showModal({
					title: '友情提示',
					content: `暂未绑定手机号，点击下方《去绑定》进行手机号绑定`,
					confirmText: '去绑定',
					confirmColor: '#2770FE',
					showCancel: true,
					success: (res) => {
						if (res.confirm) {
							uni.navigateTo({
								url: `/pages/indoor/bindphone?redirect=1`
							})
						}
					}
				})
			}else{
				uni.navigateTo({
					url: `/pages/indoor/scheme`
				})
			}
		},
		go(url) {
			uni.navigateTo({
				url
			})
		},
		renewal(url, item) {
			let data = encodeURIComponent(JSON.stringify({
				id:item.id,
				park: item.parkName,
				parkId:item.parkId,
				plateNumber: item.plateNumber,
				phoneNumber: item.phoneNumber,
				ruleId: item.ruleId,
			}))
			uni.navigateTo({
				url: `${url}?data=${data}`,
			})
		},
		async getParkInsideList() {
			let res = await parkInsideList()
			this.list = res.data || []
		}
	}
}